home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 116_01.zip / DATABASE.C < prev    next >
Text File  |  1993-06-19  |  6KB  |  442 lines

  1.  
  2. /*  programs in DATABASE.C    no changes for V 1.43   */
  3. #include "advent.h"
  4.  
  5. /*
  6.     Routine to fill travel array
  7.     for a given location.
  8. */
  9. gettrav(loc)
  10. int loc;
  11. {
  12.     int opt;
  13.     char atrav[20];
  14.  
  15.     swchfd(3);
  16.     fseek(dbuff,idx3[(loc-1)/10],0);
  17.     rdskip(dbuff,'#',(loc-1)%10+1,0);
  18.     rdskip(dbuff,'\n',1,0);
  19.     for(opt=0;opt<MAXTRAV;++opt) {
  20.         rdupto(dbuff,'\n',0,atrav);
  21.         if(atrav[0]=='#'){
  22.             travel[opt].tdest=-1;
  23.             return;
  24.         }
  25.         travel[opt].tcond=0;
  26.         initw(&travel[opt],atrav);
  27.     }
  28.     bug(33);
  29. }
  30.  
  31. /*
  32.     Function to scan a file up to a specified
  33.     point and either print or return a string.
  34. */
  35. rdupto(buff,delim,print,string)
  36. char*buff,delim,print,*string;
  37. {
  38.     int c;
  39.  
  40.     while((c=getc(buff))!=delim) {
  41.         if(c==-1)
  42.             bug(35);
  43.         if(c==EOF)
  44.             return 0;
  45.         if(c=='\015')
  46.             continue;
  47.         if(print)
  48.             putchar(c);
  49.         else
  50.             *string++=c;
  51.     }
  52.     if (!print)
  53.         *string='\0';
  54.     return 1;
  55. }
  56.  
  57. /*
  58.     Function to read a file skipping
  59.     a give character a specified number
  60.     of times, with or without repositioning
  61.     the file.
  62. */
  63. rdskip(buff,x,n,rewind)
  64. char *buff,x,rewind;
  65. int n;
  66. {
  67.     int c;
  68.  
  69.     if(rewind)
  70.         if(fseek(buff,0,0)==-1)
  71.             bug(31);
  72.     while(n--)
  73.         while((c=getc(buff))!=x)
  74.             if(c==-1 || c==EOF)
  75.                 bug(32);
  76. }
  77.  
  78. /*
  79.     routine to seek in buffered file
  80. */
  81. struct buf {
  82.     int bfd;
  83.     int nleft;
  84.     char *nextp;
  85.     char buff[128];
  86. };
  87.  
  88. fseek(iobuf,offset,code)
  89. struct buf *iobuf;
  90. int offset;
  91. char code;
  92. {
  93.     int i;
  94.  
  95.     if (code) {
  96.         for (i=0;i<offset;++i)
  97.             if(getc(iobuf)==-1)
  98.                 return(-1);
  99.     }
  100.     else {
  101.         if(seek(iobuf->bfd,offset/128,0)==-1)
  102.             return(-1);
  103.         iobuf->nleft = 0;
  104.         for (i=0;i<offset%128;++i)
  105.             if(getc(iobuf)==-1)
  106.                 return(-1);
  107.     }
  108. }
  109.  
  110. /*
  111.     Routine to switch fd's in a
  112.     file buffer to allow buffer sharing
  113. */
  114. swchfd(n)
  115. int n;
  116. {
  117.     int *fdptr;
  118.  
  119.     fdptr = dbuff;
  120.     *fdptr = fd[n];
  121. }
  122.  
  123. /*
  124.     Routine to request a yes or no answer
  125.     to a question.
  126. */
  127. yes(msg1,msg2,msg3)
  128. int msg1,msg2,msg3;
  129. {
  130.     char answer[80];
  131.  
  132.     if(msg1)
  133.         rspeak(msg1);
  134.     putchar('>');
  135.     gets(answer);
  136.     if(tolower(answer[0])=='n') {
  137.         if(msg3)
  138.             rspeak(msg3);
  139.         return 0;
  140.     }
  141.     if(msg2)
  142.         rspeak(msg2);
  143.     return 1;
  144. }
  145.  
  146. /*
  147.     Print a random message from database 6
  148. */
  149. rspeak(msg)
  150. int msg;
  151. {
  152.     if (msg == 54) {
  153.         printf("ok.\n");
  154.         return;
  155.     }
  156.     swchfd(6);
  157.     fseek(dbuff,idx6[(msg-1)/10],0);
  158.     rdskip(dbuff,'#',(msg-1)%10+1,0);
  159.     rdskip(dbuff,'\n',1,0);
  160.     rdupto(dbuff,'#',1,0);
  161. }
  162.  
  163. /*
  164.     Routine to print the message
  165.     for an item in a given state.
  166. */
  167. pspeak(item,state)
  168. int item,state;
  169. {
  170.     swchfd(5);
  171.     rdskip(dbuff,'#',item,1);
  172.     rdskip(dbuff,'/',state+2,0);
  173.     rdupto(dbuff,'/',1,0);
  174. }
  175.  
  176. /*
  177.     Print the long description of a location
  178. */
  179. desclg(loc)
  180. int loc;
  181. {
  182.     swchfd(1);
  183.     fseek(dbuff,idx1[(loc-1)/10],0);
  184.     rdskip(dbuff,'#',(loc-1)%10+1,0);
  185.     rdskip(dbuff,'\n',1,0);
  186.     rdupto(dbuff,'#',1,0);
  187. }
  188.  
  189. /*
  190.     Print the short description of a location
  191. */
  192. descsh(loc)
  193. int loc;
  194. {
  195.     swchfd(2);
  196.     fseek(dbuff,idx2[(loc-1)/10],0);
  197.     rdskip(dbuff,'#',(loc-1)%10+1,0);
  198.     rdskip(dbuff,'\n',1,0);
  199.     rdupto(dbuff,'#',1,0);
  200. }
  201.  
  202. /*
  203.     routine to look up a vocabulary word.
  204.     word is the word to look up.
  205.     val  is the minimum acceptable value,
  206.         if != 0 return %1000
  207. */
  208. vocab(word,val)
  209. char *word;
  210. int val;
  211. {
  212.     char vword[WORDSIZE];
  213.     int v;
  214.     char *wptr;
  215.     int wval;
  216.  
  217.     wval = 0;
  218.     wptr = fastverb;
  219.     while (*wptr) {
  220.         if (!strcmp(word,wptr)) {
  221.             v = fastvval[wval];
  222.             if (!val)
  223.                 return v;
  224.             if (val <= v)
  225.                 return v%1000;
  226.         }
  227.         while (*wptr++);
  228.         ++wval;
  229.     }
  230.     /* reposition file */
  231.     swchfd(4);
  232.     if(fseek(dbuff,fastvseek,0)==-1)
  233.         bug(21);
  234.     while(1) {
  235.         /* glom onto a word */
  236.         if(!rdupto(dbuff,',',0,vword))
  237.             return(-1);
  238.         /* compare words */
  239.         if (!strcmp(word,vword)){
  240.             if(!rdupto(dbuff,'\n',0,vword))
  241.                 bug(30);
  242.             v=atoi(vword);
  243.             if(!val)
  244.                 return v;
  245.             if(val<=v)
  246.                 return v%1000;
  247.         }
  248.         rdskip(dbuff,'\n',1,0);
  249.     }
  250. }
  251.  
  252. /*
  253.     Utility Routines
  254. */
  255.  
  256. /*
  257.     Routine to test for darkness
  258. */
  259. dark()
  260. {
  261.     return(!(cond[loc] & LIGHT) &&
  262.         (!prop[LAMP] ||
  263.         !here(LAMP)));
  264. }
  265.  
  266. /*
  267.     Routine to tell if an item is present.
  268. */
  269. here(item)
  270. int item;
  271. {
  272.     return(place[item]==loc || toting(item));
  273. }
  274.  
  275. /*
  276.     Routine to tell if an item is being carried.
  277. */
  278. toting(item)
  279. int item;
  280. {
  281.     return(place[item]==-1);
  282. }
  283.  
  284. /*
  285.     Routine to tell if a location causes
  286.     a forced move.
  287. */
  288. forced(atloc)
  289. int atloc;
  290. {
  291.     return(cond[atloc]==2);
  292. }
  293.  
  294. /*
  295.     Routine true x% of the time.
  296. */
  297. pct(x)
  298. int x;
  299. {
  300.     return(rand()%100 < x);
  301. }
  302.  
  303. /*
  304.     Routine to tell if player is on
  305.     either side of a two sided object.
  306. */
  307. at(item)
  308. int item;
  309. {
  310.     return(place[item]==loc || fixed[item]==loc);
  311. }
  312.  
  313. /*
  314.     Routine to destroy an object
  315. */
  316. dstroy(obj)
  317. int obj;
  318. {
  319.     move(obj,0);
  320. }
  321.  
  322. /*
  323.     Routine to move an object
  324. */
  325. move(obj,where)
  326. int obj,where;
  327. {
  328.     int from;
  329.  
  330.     from = (obj<MAXOBJ) ? place[obj] : fixed[obj];
  331.     if(from>0 && from<=300)
  332.         carry(obj,from);
  333.     drop(obj,where);
  334. }
  335.  
  336. /*
  337.     Juggle an object
  338.     currently a no-op
  339. */
  340. juggle(loc)
  341. int loc;
  342. {
  343. }
  344.  
  345. /*
  346.     Routine to carry an object
  347. */
  348. carry(obj,where)
  349. int obj,where;
  350. {
  351.     if(obj<MAXOBJ){
  352.         if(place[obj]==-1)
  353.             return;
  354.         place[obj]=-1;
  355.         ++holding;
  356.     }
  357. }
  358.  
  359. /*
  360.     Routine to drop an object
  361. */
  362. drop(obj,where)
  363. int obj,where;
  364. {
  365.     if(obj<MAXOBJ) {
  366.         if(place[obj]==-1)
  367.             --holding;
  368.         place[obj]=where;
  369.     }
  370.     else
  371.         fixed[obj-MAXOBJ]=where;
  372. }
  373.  
  374. /*
  375.     routine to move an object and return a
  376.     value used to set the negated prop values
  377.     for the repository.
  378. */
  379. put(obj,where,pval)
  380. int obj,where,pval;
  381. {
  382.     move(obj,where);
  383.     return((-1)-pval);
  384. }
  385. /*
  386.     Routine to check for presence
  387.     of dwarves..
  388. */
  389. dcheck()
  390. {
  391.     int i;
  392.  
  393.     for(i=1;i<(DWARFMAX-1);++i)
  394.         if(dloc[i]==loc)
  395.             return i;
  396.     return 0;
  397. }
  398.  
  399. /*
  400.     Determine liquid in the bottle
  401. */
  402. liq()
  403. {
  404.     int i,j;
  405.     i=prop[BOTTLE];
  406.     j=-1-i;
  407.     return liq2(i>j ? i : j);
  408. }
  409.  
  410. /*
  411.     Determine liquid at a location
  412. */
  413. liqloc(loc)
  414. int loc;
  415. {
  416.     if(cond[loc]&LIQUID)
  417.         return liq2(cond[loc]&WATOIL);
  418.     else
  419.         return liq2(1);
  420. }
  421.  
  422. /*
  423.     Convert  0 to WATER
  424.          1 to nothing
  425.          2 to OIL
  426. */
  427. liq2(pbottle)
  428. int pbottle;
  429. {
  430.     return (1-pbottle)*WATER+(pbottle>>1)*(WATER+OIL);
  431. }
  432.  
  433. /*
  434.     Fatal error routine
  435. */
  436. bug(n)
  437. int n;
  438. {
  439.     printf("Fatal error number %d\n",n);
  440.     exit();
  441. }
  442.